import { Injectable } from '@nestjs/common';
import * as si from 'systeminformation';
function toGB(value) {
  return (value / 1024 / 1024 / 1024).toFixed(2) + 'G';
}
@Injectable()
export class SystemService {
  async getSystemInfo() {
    const cpu = await si.currentLoad();
    const memory = await si.mem();
    const disk = await si.fsSize();
    const osInfo = await si.osInfo();
    const networkInterfaces = await si.networkInterfaces();
    return {
      cpu: {
        cores: cpu.cpus.length,
        userload: cpu.currentLoadUser.toFixed(2),
        systemLoad: cpu.currentLoadSystem.toFixed(2),
        idle: cpu.currentLoadIdle.toFixed(2) + '%',
      },
      memory: {
        total: toGB(memory.total),
        used: toGB(memory.used),
        free: toGB(memory.free),
        usage: ((memory.used / memory.total) * 100).toFixed(2) + '%',
      },
      disks: disk.map((d) => ({
        mount: d.mount,
        filesystem: d.fs,
        type: d.type,
        size: toGB(d.size),
        used: toGB(d.used),
        available: toGB(d.available),
        usage: d.use.toFixed(2) + '%',
      })),
      server: {
        hostname: osInfo.hostname,
        ip: networkInterfaces[0]?.ip4 || 'N/A',
        os: osInfo.distro,
        arch: osInfo.arch,
      },
    };
  }
}
